﻿using CommonTools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WeChatCore.DefineClass;

namespace WindowsWeChat.Control
{
    /// <summary>
    /// HeaderImage.xaml 的交互逻辑
    /// </summary>
    public partial class HeaderImage : UserControl
    {
        public HeaderImage()
        {
            InitializeComponent();
        }
        public static readonly DependencyProperty HeaderImagePathProperty = DependencyProperty.Register("HeaderImagePath", typeof(string), typeof(HeaderImage), new PropertyMetadata(new PropertyChangedCallback(HeaderImagePathChanged)));

        private static void HeaderImagePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //TODO
            HeaderImage hi = d as HeaderImage;
            return;
            //if (hi.HeadImage.Source != null)
            //    return;
            string path = hi.HeaderImagePath;
            new Task(() =>
            {
                try
                {
                   byte[] b = (byte[])HttpHelper.HttpMethods.GetFile(UrlDefine.RootUrl + path, "", CommonDefine.Cookies).ContentData;
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        MemoryStream ms = new MemoryStream(b);
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = ms;// new FileStream();
                        bi.EndInit();
                        hi.HeadImage.Source = bi;
                    }));
                }
                catch (Exception ex)
                {
                    LogWriter.Write(ex.Message, LogPathDefine.ExceptionLogPath);
                }
            }).Start();
        }
        public string HeaderImagePath
        {
            get
            {
                return (string)GetValue(HeaderImagePathProperty);
            }
            set
            {
                SetValue(HeaderImagePathProperty, value);
            }
        }
    }
}
